package  com.update
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	/**
	 * 此类用于验证作弊行为
	 *
	 * @author flashyiyi
	 *
	 */
	public class CheckUpdate extends Update
	{
		private var blur : int = 20;
		private var prevDate : Number; //上次记录的时间
		private var prevTime : int; //上次记录的Time
		private var isInit : Boolean = false;
		private static var instance : CheckUpdate;

		public static function getInstance() : CheckUpdate
		{
			if(instance == null)
			{
				instance = new CheckUpdate();
				instance.init();
			}
			return instance;
		}

		/**
		 * 激活变速齿轮验证
		 *
		 * @param interval	检测间隔
		 *
		 */
		public function start(interval : int = 5000, blur : int = 100) : void
		{
			isInit = false;
			this.blur = blur;
			this.frameInterval = interval;
			this.registerd();
		}

		override public function update(delay : uint) : void
		{
			var nextTime : int = getTimer();
			var newDate : Number = new Date().getTime();
			var interval : int = nextTime - prevTime;


			if(isInit && Math.abs(interval - (newDate - prevDate)) > blur)
			{
				defaultHackHandler(Math.abs(interval - (newDate - prevDate)));
			}

			isInit = true;
			prevDate = newDate;
			prevTime = nextTime;
		}

		public function defaultHackHandler(time : int) : void
		{
			//Alert.show("你已断开连接!!!");
			//Print.error("请不要使用加速!", time);
			//GameSocket.instance.close();
			stop();
		}

		public function stop() : void
		{
			this.unRegisterd();
		}
	}
}